/*
  *     Copyright (c)  Wuxi Airborne Energy Saving Technologies Co., Ltd.
  *      This program is not a free software; All rights reserved.
  *     history:
  *     Author: han.qin@airborne-es.com  created 
  *                chuanqi.zheng@airbone-es.com modify 2012-6-27 
  *               add K1 key for self-test use  chuanqi.zheng@airborne-es.com 20120706
  *
  */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
 
#define USE_DEBUG
#ifdef   USE_DEBUG
#define __func__ __FUNCTION__
#define DEBUG_ERR(fmt, args...) printf("\033[46;31m[%s:%d]\033[0m "#fmt" errno=%d, %m\r\n", __func__, __LINE__, ##args, errno, errno)
#define DEBUG_INFO(fmt, args...) printf("\033[33m[%s:%d]\033[0m "#fmt"\r\n", __func__, __LINE__, ##args)
#else
#define DEBUG_ERR(fmt, ...)
#define DEBUG_INFO(fmt, ...)
#endif


/**
  *     mini2440 has 6 buttons on board. The button driver is included in
  *     linux-2.6.32.2\drivers\char\mini2440_buttons.c. Every buttons is 
  *     registered with an irq. The read method is a blocking IO, which results relates
  *     on irq event happen. So only when a key is pressed down or up, the IRQ is 
  *     Invoked, and read gets results, like press down event return with ASCII '2'; 
  *     press up event return with ASCII '0'; In our App, we use key 3 for monitor 
  *     that the key is firstly pressed and then up again. After this event is detected, 
  *     A value "0x02" is write into /home/AB/BS/buttons_event.txt.
  *     
  */
int s3c2440_button_monitor(void)
{
    FILE *stream;
    char readSign = '0';
    char writeSignK3 = '3';  //for daiwei
    char writeSignK2 = '2'; //xinfeng Air-pressure test
    char writeSignK1 = '1';  //for self-test
    int buttons_fd;
    int ret;
    char current_buttons[6] = {'2', '2', '2', '2', '2', '2'};
    int i,j,k; 
    
    buttons_fd = open("/dev/buttons", 0);
    if (buttons_fd < 0) {
        DEBUG_ERR("open device buttons failed!\n");
        exit(1);
    } 

    i = 2;   // check if k3/k2/k1 is prssed
    j = 0;
    k = 1;
    for (;;) {     
        ret = read(buttons_fd, current_buttons, sizeof(current_buttons)) ;
        if (ret == -1 ) {
            DEBUG_ERR("#1 read buttons failed: %d\n", ret);
        }
        
        if (current_buttons[i] == '1') {
            DEBUG_INFO("k3 is pressed down\n" );
            ret = read(buttons_fd, current_buttons, sizeof(current_buttons)) ;
            if (ret == -1 ) {
                DEBUG_ERR("#2 read buttons failed: %d\n", ret);
            }
                       
            if (current_buttons[i] == '0') {
                DEBUG_INFO("k3 is pressed up\n" );
                DEBUG_INFO("\nThe key %d has been triggerred\n",i+1);

               if((stream = fopen("/home/AB/BS/rom/button_event.txt","w+")) == NULL) {
                    DEBUG_ERR("Cannot open /home/AB/BS/rom/button_event.txt.\n"); 
               }  

               fwrite(&writeSignK3, sizeof(char), 1, stream);					   
               fseek(stream, 0, SEEK_SET);
               fread(&readSign, sizeof(char), 1, stream);
               printf("Value %d is read from file button_event.txt\n", readSign);
               fclose(stream);
            } 
            
            continue;
        }

        if( current_buttons[k] == '1' ) {
             DEBUG_INFO("k2 is pressed down\n" );
            ret = read(buttons_fd, current_buttons, sizeof(current_buttons)) ;
            if (ret == -1 ) {
                DEBUG_ERR("#2 read buttons failed: %d\n", ret);
            }
                       
            if (current_buttons[k] == '0') {
                 DEBUG_INFO("k2 is pressed up\n" );
                 DEBUG_INFO("\nThe key %d has been triggerred\n", k+1);
                
               if((stream = fopen("/home/AB/BS/rom/button_event.txt","w+")) == NULL) {
                    DEBUG_ERR("Cannot open /home/AB/BS/rom/button_event.txt.\n"); 
               }  

               fwrite(&writeSignK2, sizeof(char), 1, stream);					   
               fseek(stream, 0, SEEK_SET);
               fread(&readSign, sizeof(char), 1, stream);
               printf("Value %d is read from file button_event.txt\n", readSign);
               fclose(stream);
            }

            continue;
        }

        if( current_buttons[j] == '1' ) {
             DEBUG_INFO("k1 is pressed down\n" );
            ret = read(buttons_fd, current_buttons, sizeof(current_buttons)) ;
            if (ret == -1 ) {
                DEBUG_ERR("#2 read buttons failed: %d\n", ret);
            }
                       
            if (current_buttons[j] == '0') {
                 DEBUG_INFO("k1 is pressed up\n" );
                 DEBUG_INFO("\nThe key %d has been triggerred\n", j+1);
                
               if((stream = fopen("/home/AB/BS/rom/button_event.txt","w+")) == NULL) {
                    DEBUG_ERR("Cannot open /home/AB/BS/rom/button_event.txt.\n"); 
               }  

               fwrite(&writeSignK1, sizeof(char), 1, stream);					   
               fseek(stream, 0, SEEK_SET);
               fread(&readSign, sizeof(char), 1, stream);
               printf("Value %d is read from file button_event.txt\n", readSign);
               fclose(stream);
            }

            continue;
        }
    }

    close(buttons_fd);

    return 0;
}



int main(void)
{ 
     s3c2440_button_monitor();
     return 0;
}
